
var models = require('../models');
var hash = require('../hash.js').hash;
var fs = require('fs');
var url=require('url');
var userModel = models.userModel;
var callUserModel = models.callUserModel;
var pdfUserModel = models.pdfUserModel;
var pdfModel = models.pdfModel;
var pieTime = function(){
  var curTime = new Date();
  var year = curTime.getFullYear();
  var month = curTime.getMonth()+1;
  if(month<10){
    month = '0'+month;
  }
  var date = curTime.getDate();
  if(date<10){
    date = '0'+date;
  }
  var hour = curTime.getHours();
  if(hour<10){
    hour = '0'+hour;
  }
  var minute = curTime.getMinutes();
  if(minute<10){
    minute = '0'+minute;
  }
  var second = curTime.getSeconds();
  if(second<10){
    second = '0'+second;
  }
  var result = year+'-'+month+'-'+date+' '+hour+':'+minute+':'+second;
  return result;
}
function randomChar(l){
	var x="123456789poiuytrewqasdfghjklmnbvcxzQWERTYUIPLKJHGFDSAZXCVBNM";
 	var tmp="";
 	for(var i=0;i< l;i++) {
 		tmp += x.charAt(Math.ceil(Math.random()*100000000)%x.length);
 	}
 	return tmp;
}
function Trim(str,is_global){
	var result; 
 	result = str.replace(/(^\s+)|(\s+$)/g,"");
 	if(is_global.toLowerCase()=="g"){
 		result = result.replace(/\s/g,"");
 	}
 	return result;
}
exports.index = function(req,res){
	res.render('index.ejs')
}
exports.upload = function(req,res){
	//别把这个函数删了，这是网络实习课上的作业
	if(!req.files||!req.files.file||!req.files.file.name){
		res.send({
			status:'error',
			message:'请上传头像'
		});
		return;
	}
	var tmpPath = req.files.file.path;
	var userId = req.body.account;
	var date = new Date();
	var now = ''+date.getHours()+date.getMinutes()+date.getSeconds();
	var newName = Trim(req.files.file.name,'g');
	var targetPath = 'public/img/'+userId+'/'+now+newName;
	var newSrc = '/img/'+userId+'/'+now+newName;
	fs.exists('public/img/'+userId,function(exists){
		if(!exists){
			fs.mkdir('public/img/'+userId,function(err){
				if(!err){
					fs.rename(tmpPath,targetPath,function(err){
						if(err){
							res.send({'status':'error','message':'move file error'});
						}
						else{
							userModel.findOne({account:userId},function(err,user){
								if(err){
									res.send({
										status:'error',
										message:JSON.stringify(err)
									});
								}
								else{
									if(user){
										user.avatar = newSrc;
										user.save(function(err){
											if(err){
												res.send({
													status:'error',
													message:JSON.stringify(err)
												});
											}
											else{
												res.send({
													status:'success',
													src:newSrc
												});
											}
										})
									}
									else{
										res.send({
											status:'error',
											message:'userId不合法，对应用户不存在'
										});
									}
								}
							})
						}
					})
				}
				else{
					res.send({'status':'error','message':'mkdir error'});
				}
			})
		}
		else{
			fs.rename(tmpPath,targetPath,function(err){
				if(err){
					res.send({'status':'error','message':'move file error'});
				}
				else{
					userModel.findOne({account:userId},function(err,user){
						if(err){
							res.send({'status':'error','message':'connect database error'});
						}
						else{
							if(user){
								user.avatar = newSrc;
								user.save(function(err){
									if(err){
										res.send({'status':'error','message':'save user error'});
									}
									else{
										res.send({'status':'success','src':newSrc});
									}
								})
							}
							else{
								res.send({'status':'error','message':'userId不合法，对应用户不存在	'});
							}
						}
					})
				}
			})
		}
	})
}